package com.leyou.item.controller;


import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import com.leyou.utils.ResponseEntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类管理页 分类的三级查询
     * @param pid 分类id
     * @return 子分类
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        List<Category> categories = this.categoryService.queryListByParent(pid);
        if (categories != null && 0 != categories.size()) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     *品牌管理界面 修改品牌弹窗数据回显
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> categoryList = this.categoryService.queryByBrandId(bid);
        return ResponseEntityUtils.selectResultStatus(categoryList);
    }


    /**
     * search服务调用  根据商品分类id查询名称
     * @param ids 要查询的分类id集合
     * @return 多个名称的集合
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids){
        List<String > list = this.categoryService.queryNamesByIds(ids);
        return ResponseEntityUtils.selectResultStatus(list, HttpStatus.NOT_FOUND);
    }



    /**
     * 根据3级分类id，查询1~3级的分类
     * @param cid
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("cid") Long cid) {
        List<Category> categories = this.categoryService.queryAllByCId3(cid);
        return ResponseEntityUtils.selectResultStatus(categories);
    }
}
